#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'/../tests/Test.php';
require_once APPPATH.'/../tests/models/TestTank.php';
require_once APPPATH.'/../tests/models/TestSource.php';
require_once APPPATH.'/../tests/models/TestOrganization.php';
require_once 'tank/Discriminator.php';

AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();
$d = new Discriminator();

// setup
$o1 = new TestOrganization();
$o1->save();
$o2 = new TestOrganization();
$o2->save();
$s = new TestSource();
$s->save();
$t = new TestTank();
$t->tank_user_id = 1;
$t->tank_type = Tank::$TYPE_FB;
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->TankSource[0]->src_id = $s->src_id;
$t->save();
$ts = $t->TankSource[0];
$t->clearRelated();


plan(38);

/**********************
 * 1) add source to new org
 */
$t->tank_org_id = $o1->org_id;
$t->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 'new org - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 'new org - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'new org - tsrc_status' );
is( $d->done_count, 1, 'new org - done count' );
is( count($s->SrcOrg), 1, 'new org - 1 SrcOrg' );
$so = $s->SrcOrg[0];
$s->clearRelated();
is( $so->so_org_id, $o1->org_id, 'new org - org_id' );
is( $so->so_status, SrcOrg::$STATUS_OPTED_IN, 'new org - opted in' );
is( $so->so_home_flag, true, 'new org - home flag' );


/**********************
 * 2) re-join a source to opted-out organization
 */
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->save();
$ts->tsrc_status = TankSource::$STATUS_NEW;
$ts->tsrc_errors = null;
$ts->save();
$so->so_status = SrcOrg::$STATUS_OPTED_OUT;
$so->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 're-opt - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 're-opt - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 're-opt - tsrc_status' );
is( $d->done_count, 1, 're-opt - done count' );
is( count($s->SrcOrg), 1, 're-opt - 1 SrcOrg' );
$so = $s->SrcOrg[0];
$s->clearRelated();
is( $so->so_org_id, $o1->org_id, 're-opt - org_id' );
is( $so->so_status, SrcOrg::$STATUS_OPTED_IN, 're-opt - opted in' );
is( $so->so_home_flag, true, 're-opt - home flag' );


/**********************
 * 3) join source to a second organization
 */
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->tank_org_id = $o2->org_id;
$t->save();
$ts->tsrc_status = TankSource::$STATUS_NEW;
$ts->tsrc_errors = null;
$ts->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, '2-opt - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, '2-opt - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, '2-opt - tsrc_status' );
is( $d->done_count, 1, '2-opt - done count' );
is( count($s->SrcOrg), 2, '2-opt - 2 SrcOrgs' );
$so = $s->SrcOrg[0];
$so2 = $s->SrcOrg[1];
$s->clearRelated();
is( $so->so_org_id, $o1->org_id, '2-opt - org_id 1' );
is( $so2->so_org_id, $o2->org_id, '2-opt - org_id 2' );
is( $so->so_status, SrcOrg::$STATUS_OPTED_IN, '2-opt - opted in 1' );
is( $so2->so_status, SrcOrg::$STATUS_OPTED_IN, '2-opt - opted in 2' );
is( $so->so_home_flag, true, '2-opt - home flag 1' );
is( $so2->so_home_flag, false, '2-opt - home flag 2' );


/**********************
 * 4) re-join source to duplicate org
 */
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->tank_org_id = $o2->org_id;
$t->save();
$ts->tsrc_status = TankSource::$STATUS_NEW;
$ts->tsrc_errors = null;
$ts->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 'dup-opt - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 'dup-opt - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'dup-opt - tsrc_status' );
is( $d->done_count, 1, 'dup-opt - done count' );
is( count($s->SrcOrg), 2, 'dup-opt - 2 SrcOrgs' );
$so = $s->SrcOrg[0];
$so2 = $s->SrcOrg[1];
$s->clearRelated();
is( $so->so_org_id, $o1->org_id, 'dup-opt - org_id 1' );
is( $so2->so_org_id, $o2->org_id, 'dup-opt - org_id 2' );
is( $so->so_status, SrcOrg::$STATUS_OPTED_IN, 'dup-opt - opted in 1' );
is( $so2->so_status, SrcOrg::$STATUS_OPTED_IN, 'dup-opt - opted in 2' );
is( $so->so_home_flag, true, 'dup-opt - home flag 1' );
is( $so2->so_home_flag, false, 'dup-opt - home flag 2' );

